package com.woniu.adapter;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.woniu.outlet.elasticSearch.GoldESDao;
import com.woniu.outlet.elasticSearch.po.Gold;
import com.woniu.outlet.mysql.GoldMysqlMapper;
import com.woniu.outlet.mysql.po.GoldMysql;
import com.woniu.outlet.mysql.po.GoldMysqlExample;
import com.woniu.outlet.redis.po.GoldRela;
import com.woniu.outlet.redis.GoldRelaRedisDao;
import com.woniu.util.DateUtil;
import com.woniu.util.GoldUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.net.URISyntaxException;
import java.util.*;

/**
 * @description:
 * @author: my
 * @time: 2021/4/22 15:37
 */
@Slf4j
@Component
public class GoldAdapter {
    private GoldESDao goldESDao;
    private GoldUtils goldUtils;
    private GoldRelaRedisDao goldRelaRedisDao;
    private GoldMysqlMapper goldMapper;
    private DateUtil dateUtil;
    private StringRedisTemplate redisTemplate;


    public GoldAdapter(GoldUtils goldUtils, GoldESDao goldESDao,
                       GoldRelaRedisDao goldRelaRedisDao,GoldMysqlMapper goldMapper,
                       DateUtil dateUtil,StringRedisTemplate redisTemplate) {
        this.goldESDao = goldESDao;
        this.goldUtils = goldUtils;
        this.goldRelaRedisDao = goldRelaRedisDao;
        this.goldMapper = goldMapper;
        this.dateUtil = dateUtil;
        this.redisTemplate = redisTemplate;
    }

    @Transactional
    public List<Gold> getAllGold(){
        Iterable<Gold> all = goldESDao.findAll();
        return Lists.newArrayList(all);
    }

    @Transactional
    public List<GoldMysql> getGoldGraph(String type,Integer month){
        Date time = dateUtil.getDateByMonth(month);
        GoldMysqlExample example = new GoldMysqlExample();
        example.or().andTypeEqualTo(type).andUpdatetimeGreaterThan(time);
        return goldMapper.selectByExample(example);
    }

    @Transactional
    public Gold getGoldDetail(String type) throws Exception {
        Optional<Gold> opt = goldESDao.findById(type);
        try {
            Gold gold = opt.orElseThrow(RuntimeException::new);
            log.debug("从elasticsearch获取数据成功");
            return gold;
        } catch (RuntimeException e) {
            List<Gold> golds = goldUtils.queryGoldsFromNet();
            //更新elasticsearch数据
            goldESDao.deleteAll();
            goldESDao.saveAll(golds);
            log.debug("将从接口处获取的数据存入elasticsearch");
            Optional<Gold> byId = goldESDao.findById(type);
            Gold gold = byId.orElseThrow(RuntimeException::new);
            log.debug("从elasticsearch获取数据");
            log.debug(gold.getTypename());
            return gold;
        }
    }

    @Transactional
    public List<GoldRela> getGolds() throws URISyntaxException {
        List<GoldRela> all = (List<GoldRela>) goldRelaRedisDao.findAll();
        if (all.size() > 0){
            log.debug("从redis获取黄金种类信息");
            return all;
        }
//        List<Gold> golds = goldUtils.queryGoldsFromNet();
        Iterable<Gold> golds = goldESDao.findAll();
        List<Gold> list = Lists.newArrayList(golds);
        goldUtils.saveGolds(list);
        log.debug("接口处获取数据量：{}",list.size());
        List<GoldRela> goldRelas = new ArrayList<>();
        for (Gold gold : golds) {
            GoldRela goldRela = new GoldRela();
            goldRela.setType(gold.getType());
            goldRela.setTypename(gold.getTypename());
            goldRelas.add(goldRela);
        }
        goldRelaRedisDao.saveAll(goldRelas);
        log.debug("从接口处获取黄金种类信息");
        return goldRelas;
    }

}
